﻿Public Class UserControl1

    Private Sub AgoresBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AgoresBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.AgoresBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Hard_oneDataSet)

    End Sub

    Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AgoresTableAdapter.Fill(Me.Hard_oneDataSet.agores)
    End Sub
    Private Function SaveChanges() As Boolean
        If ValidateData() Then
            Me.TableAdapterManager.UpdateAll(Me.Hard_oneDataSet)
            Return True
        End If
        Return False
    End Function

    Protected Overloads Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        AddHandler Me.ParentForm.FormClosing, AddressOf ParentForm_FormClosing
    End Sub
    Private Function ValidateData() As Boolean
        If Me.Validate() Then
            Try
                AgoresBindingSource.EndEdit()
                Return True
            Catch ex As Exception
                If MessageBox.Show( _
                  "Invalid data was entered. Cancel edits?", _
                  "Agores", MessageBoxButtons.YesNo, _
                  MessageBoxIcon.Error) = DialogResult.Yes Then

                    AgoresBindingSource.CancelEdit()
                End If
                Return False
            End Try
        End If
        Return False
    End Function
    Private Sub ParentForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
        If ValidateData() Then
            If Me.Hard_oneDataSet.agores.GetChanges _
            IsNot Nothing Then
                Dim response As Integer = _
                MessageBox.Show( _
                "Do you want to save your changes?", _
                "Changes have been made", _
                MessageBoxButtons.YesNoCancel, _
                MessageBoxIcon.Warning)
                If response = DialogResult.Yes Then
                    If SaveChanges() Then
                        MessageBox.Show("Your changes were saved", _
                        "Agores", MessageBoxButtons.OK)
                    Else
                        e.Cancel = True
                    End If
                ElseIf response = DialogResult.Cancel Then
                    e.Cancel = True
                End If
            End If
        End If
    End Sub

End Class